package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.service.CustomerService;
import com.atguigu.jxc.service.SupplierService;

import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: CustomerController
 * Description:
 * Date: 2022/1/15 20:36
 *
 * @version:
 * @since JDK 1.8
 */

@RestController
@RequestMapping("/customer")
public class CustomerController {


    @Autowired
    private CustomerService customerService;


    @PostMapping("/list")
    public Map<String,Object> listPage(Integer page, Integer rows, String  customerName){

        Page<Customer> customerList = customerService.selectPage(page,rows,customerName);


        HashMap<String, Object> stringObjectHashMap = new HashMap<>();

        stringObjectHashMap.put("total",customerList.getTotal());

        stringObjectHashMap.put("rows",customerList.getResult());
        return stringObjectHashMap;
    }


    @PostMapping("/save")
    public ServiceVO insertOrUpdateCustomer(Customer customer){

        try {
            customerService.insertOrUpdateCustomer(customer);
            return new ServiceVO<>(100,"请求成功");

        }catch (Exception e){
            e.printStackTrace();
        }

        return new ServiceVO<>(101,"添加或修改失败");

    }


    @PostMapping("/delete")
    public ServiceVO deleteCustomer(String ids){


        try {
            customerService.deleteCustomerByIds(ids);
            return new ServiceVO<>(100,"请求成功");

        }catch (Exception e){
            e.printStackTrace();
        }

        return new ServiceVO<>(101,"删除失败");
    }



    @PostMapping("/getComboboxList")
    public List<Customer> getComboboxList(String q){



        List<Customer>customerList =  customerService.getComboboxList(q);
        return customerList;





    }




}



